<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .div1 {
            width: 300px;
            height: 300px;
            background-color: #ccc;
            padding: 30px;
            border: 5px solid red;
            position: absolute;
        }

        .box1 {
            width: 300px;
            height: 300px;
            padding: 100px;
            margin-left: 400px;
            position: relative;
            border: 100px solid #000;
            background-color: pink;
        }

        .box2 {
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head>

<body>
    <div class="div1">
        div1
        <div class="div2">
            div2
        </div>
    </div>

    <div class="box1">
        <div class="box2" style="left: 10px"></div>
    </div>
    <script>
        var div1 = document.getElementsByClassName('div1')[0]
        // offset属性都是只读 不能修改
        // 作用：ele.style.width只能拿到行内的样式 要拿到元素的width要用offsetWidth
        // offsetwidth = width +2padding +2border   height同理
        console.log(div1.offsetWidth, div1.offsetHeight);

        var div2 = document.getElementsByClassName('div2')[0]
        //  offsetParent 获取当前元素的最近定位（absolute relative fixed）父元素 如果没有定位的父元素那就找body
        console.log(div2.offsetParent, div1.offsetParent);


        var box2 = document.getElementsByClassName("box2")[0];

        //offsetTop和offsetLeft 当前元素相对于其定位父元素的垂直 水平偏移量 从定位父元素的padding开始算 border不算 没有定位父元素就找body
        console.log(box2.offsetLeft,box2.offsetTop);  //100
    </script>
</body>

</html>